/**
 * JAVACC DEMO 1.0
 * @copy right zbxsoft company All rights reserved. 
 * @Package com.zbxsoft.platform.template.helper  
 */
package com.apache.design.template.helper;

import com.apache.design.template.FreemarkerHelper;
import com.apache.design.util.DesignEntity;
import com.apache.design.util.FileOperate;
import com.apache.design.util.GZipUtil;
import com.apache.info.entity.PubSortInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description:  生成配置实现类
 */
public class SpringTempManager extends SupperTemplateManager {

	@SuppressWarnings("unchecked")
	public boolean doInvoke(DesignEntity entity) {
		boolean mark = false;
		List<PubSortInfo> list = entity.getTabs();
		String jarPath = entity.getPackPath();
		String projectName = entity.getProjectName();
		String xmlPath = entity.getFilePath();
		String sysName = entity.getSysEname();
		try {
			createSpring(list, jarPath, projectName, xmlPath, sysName);
			mark = true;
		} catch (Exception e) {
			e.printStackTrace();
			log.error("生成配置失败", e);
		}
		return mark;
	}

	private void createSpring(List<PubSortInfo> list, String jarPath, String projectName, String filePath,
			String sysName) throws Exception {
		String xmlPath = filePath + "/" + projectName + "/src/main/resources/spring/";
		FileOperate.getInstance().newCreateFolder(xmlPath);
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("packPath", jarPath);
		data.put("reqName", sysName);
		data.put("sysName", GZipUtil.toUpperFirst(sysName));
		data.put("sorts", list);
		String result = FreemarkerHelper.instance().getTemplateStr("config/spring/applicationContext.inc", data);
		FileOperate.getInstance().newCreateFile(xmlPath + "applicationContext-" + sysName + ".xml", result);

		data.put("dataSource",sysName);
		result = FreemarkerHelper.instance().getTemplateStr("config/spring/datasource-config.inc", data);
		FileOperate.getInstance().newCreateFile(xmlPath + "datasource-config.xml", result);

		xmlPath = filePath + "/" + projectName + "/src/main/resources/";
		result = FreemarkerHelper.instance().getTemplateStr("config/spring-context.inc", data);
		FileOperate.getInstance().newCreateFile(xmlPath + "spring-context.xml", result);
	}

}
